<style scoped lang="less">
.ontent-main {
}
.search-wrapper {
  margin-left: 270px;
  // margin-top: 15px;
  overflow: auto;

  .ivu-form-item {
    margin-bottom: 0px;
    vertical-align: top;
    zoom: 1;
  }
}
</style>
<template>
  <div class="content-main">
    <div>
      <Card :bordered="false">
        <!-- 数据表格 -->
        <div>
          <Table
            size="small"
            :border="true"
            :stripe="true"
            :columns="tableHeader"
            :data="tableData"
            :height="height"
            style="margin-top: 12px"
          ></Table>
        </div>
      </Card>
    </div>
    <div>
      <navigation :search.sync="doSearch" :pageSizeOpts="[9,18,27,36]" @navpage="pagerCallback"></navigation>
    </div>
  </div>
</template>
<script>
import navigation from '@/view/components/navigation/navigation'
import { getToken } from '@/libs/util'
import Vue from 'vue'

export default {
  components: {
    navigation
    // ComOpenRoom
  },
  props: ['roomId', 'ownInfo', 'roomInfo'],
  data () {
    return {
      loadMoreValue: 0,
      condition: {
        roomId: this.roomId
      },
      tableData: [],
      doSearch: false,
      tableHeader: [
        // {
        //   type: "selection",
        //   minwidth: 30,
        //   align: "center",
        //   fixed: "left"
        // },

        // {
        //   // 状态
        //   title: "状态",
        //   key: "status",
        //   minWidth: 90,
        //   ellipsis: true,
        //   align: "center",
        //   render: (h, params) => {
        //     let status = params.row.status;
        //     if (status === 1) {
        //       return h(
        //         "span",
        //         { style: { color: "#2d8cf0", fontWeight: "bold" } },
        //         "正常"
        //       );
        //     } else {
        //       return h(
        //         "span",
        //         { style: { color: "#ed4014", fontWeight: "bold" } },
        //         "已删除"
        //       );
        //     }
        //   }
        //   // fixed: "left"
        // },
        {
          title: '文件名',
          key: 'name',
          minWidth: 200,
          ellipsis: true,
          align: 'center'
          // fixed: "left"
        },
        {
          title: '类型',
          key: 'type',
          minWidth: 100,
          ellipsis: true,
          align: 'center'
        },
        {
          title: '上传日期',
          key: 'createdAt',
          minWidth: 150,
          ellipsis: true,
          align: 'center'
        },
        {
          title: '操作',
          minWidth: 200,
          align: 'center',
          // fixed: "right",
          render: (h, params) => {
            // <a href="/images/myw3schoolimage.jpg" download="w3logo">
            let btns = []
            btns.push(
              h(
                'a',
                {
                  attrs: {
                    // 属性 用attrs
                    href: params.row.url,
                    download: params.row.name,
                    target: '_black'
                  },
                  style: {
                    marginRight: '5px'
                  }
                },
                '下载文件'
              )
            )
            return h('div', btns)
          }
        }
      ],

      height: 420
    }
  },

  methods: {
    // 添加教师操作
    addTeacherOperator () {
      if (
        this.ownInfo.isOwnRoom === true &&
        this.ownInfo.roleKey === 'teacher'
      ) {
        this.tableHeader.splice(this.tableHeader.length - 1, 1, {
          title: '操作',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            let btns = []
            btns.push(
              h(
                'a',
                {
                  attrs: {
                    // 属性 用attrs
                    href: params.row.url,
                    download: params.row.name,
                    target: '_black'
                  },
                  style: {
                    marginRight: '5px'
                  }
                },
                '下载文件'
              )
            )
            btns.push(
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '确认',
                        content: '确认删除',
                        onOk: () => {
                          this.deletFile(params.row.tid)
                        }
                      })
                    }
                  }
                },
                '删除'
              )
            )
            return h('div', btns)
          }
        })
      }
    },

    // 删除文件
    deletFile (fileId) {
      debugger
      this.$httpUtil.asyncDelete(
        'web/raroom/file/delete',
        { id: fileId },
        res => {
          debugger
          if (res.data.code === 200) {
            this.$Message.success(res.data.msg)
            this.queryMethod()
          } else {
            this.$Message.error(res.data.msg)
          }
        }
      )
    },

    // 显示专业
    showModal (dataList, title, key) {
      if (dataList && dataList.length > 0) {
        this.$Modal.info({
          // title:title,
          render: h => {
            let tags = []
            dataList.forEach(item => {
              tags.push(h('tag', { props: { color: 'green' } }, item[key]))
            })
            return h('div', tags)
          }
        })
      }
    },
    // 搜索重置
    handleResetBtn () {
      this.$refs['searchForm'].resetFields()
      this.condition = {
        roomId: this.roomId
      }
      this.createdDateRange = []
      this.queryMethod()
    },

    // 点击翻页
    pagerCallback (page, search) {
      let option = {
        url: 'web/raroom/file/queryByCondition',
        pageData: page,
        success: response => {
          this.tableData = response.data.records
        }
      }
      if (search) {
        option.params = {
          data: this.condition
        }
      }
      this.$httpUtil.pageSearch(option)
    },
    dateFormat (time) {
      if (time) {
        let date = new Date(time)
        return date.format('yyyy-MM-dd')
      } else {
        return ''
      }
    },
    queryMethod () {
      this.doSearch = true
    },

    // 初始化方法
    init () {
      if (this.roomStatus === 1) {
        this.addTeacherOperator()
      }
      this.queryMethod()
    }
  },
  computed: {
    roomStatus () {
      return this.roomInfo.status
    }
  },
  mounted () {
    this.init()
  },
  watch: {}
}
</script>
